class_name Tank extends Node3D

@onready var camera_3d = $Camera3D
@onready var health_bar = $SubViewport/Box/HealthBar
@onready var nick = $SubViewport/Box/Nick
@export var hull_type: TankGlobals.Hull = TankGlobals.Hull.WASP
@export var turret_type: TankGlobals.Turret = TankGlobals.Turret.SMOKY
@onready var max_health: int
@onready var base_damage: int

var nick_name: String

# Hulls
var wasp = preload("res://Scenes/tank/hulls/Wasp.tscn")
var hunter = preload("res://Scenes/tank/hulls/Hunter.tscn")
# Turrets
var smoky = preload("res://Scenes/tank/turrets/Smoky.tscn")
var twins = preload("res://Scenes/tank/turrets/Twins.tscn")

var tank_hull: TankHull3D
var tank_turret: TankTurret3D

func _enter_tree():
	$".".set_multiplayer_authority(str(name).to_int())

func _ready():
	respawn()

@rpc("any_peer", "call_local")
func respawn():
	if tank_hull != null:
		tank_hull.queue_free()
	if tank_turret != null:
		tank_turret.queue_free()
	
	match hull_type:
		TankGlobals.Hull.WASP:
			tank_hull = wasp.instantiate()
		TankGlobals.Hull.HUNTER:
			tank_hull = hunter.instantiate()
	add_child(tank_hull)
	
	match turret_type:
		TankGlobals.Turret.SMOKY:
			tank_turret = smoky.instantiate()
		TankGlobals.Turret.TWINS:
			tank_turret = twins.instantiate()
	tank_hull.add_child(tank_turret)
	
	var turret_pivot: Node3D = tank_hull.find_child('TurretPivot')
	if turret_pivot != null:
		tank_turret.position = turret_pivot.position
		tank_turret.rotation = Vector3(0, PI, 0)

	var camera_pivot: Node3D = tank_hull.find_child('CameraPivot')
	camera_3d.global_position = camera_pivot.global_position
	camera_3d.look_at(turret_pivot.position)
	camera_3d.reparent(tank_turret)
	
	tank_hull.position = position
	base_damage = tank_turret.damage
	max_health = tank_hull.health
	update_health_bar(max_health, max_health)
	nick.text = nick_name
	tank_hull.health_changed.connect(update_health_bar)
	tank_turret.damage = base_damage
	set_multiplayer_authority(str(name).to_int())

func _process(delta):
	if not is_multiplayer_authority(): return
	camera_3d.current = true
	if Input.is_action_pressed("turret_left"):
		tank_turret.rotate_left(delta)
	if Input.is_action_pressed("turret_right"):
		tank_turret.rotate_right(delta)
	if Input.is_action_pressed("fire"):
		tank_turret.fire()
	tank_hull.drive_direction = get_drive_direction()

func _unhandled_input(event):
	if not is_multiplayer_authority(): return
	if Input.is_action_just_pressed("mute"):
		var music_bus = AudioServer.get_bus_index("Master")
		AudioServer.set_bus_mute(music_bus, not AudioServer.is_bus_mute(music_bus))
	if Input.is_action_just_pressed('reset'):
		respawn.rpc()

	if Input.is_key_pressed(KEY_1):
		hull_type = TankGlobals.Hull.WASP
	if Input.is_key_pressed(KEY_2):
		hull_type = TankGlobals.Hull.HUNTER
	if Input.is_key_pressed(KEY_3):
		turret_type = TankGlobals.Turret.SMOKY
	if Input.is_key_pressed(KEY_4):
		turret_type = TankGlobals.Turret.TWINS

func get_drive_direction() -> TankGlobals.DriveDirection:
	var direction: TankGlobals.DriveDirection = TankGlobals.DriveDirection.P
	if Input.is_action_pressed("move_forward"):
		direction = TankGlobals.DriveDirection.F
	if Input.is_action_pressed("move_backward"):
		direction = TankGlobals.DriveDirection.B
	if Input.is_action_pressed("turn_left"):
		match direction:
			TankGlobals.DriveDirection.F:
				direction = TankGlobals.DriveDirection.FL
			TankGlobals.DriveDirection.B:
				direction = TankGlobals.DriveDirection.BL
			_: 
				direction = TankGlobals.DriveDirection.L
	if Input.is_action_pressed("turn_right"):
		match direction:
			TankGlobals.DriveDirection.F:
				direction = TankGlobals.DriveDirection.FR
			TankGlobals.DriveDirection.B:
				direction = TankGlobals.DriveDirection.BR
			_: 
				direction = TankGlobals.DriveDirection.R
	return direction

func update_health_bar(health_value, max_health):
	health_bar.max_value = max_health
	health_bar.value = health_value
